class CategoriesController < ApplicationController

  def index
    @active_section = Section.find_by_short_name("Categorias")
    @categories = Category.where(:parent_category_id => nil).order(:section_order,:category_order,:name)
  end

  # GET /categories/new
  # GET /categories/new.xml
  def new
    @active_section = Section.find_by_short_name("Categorias")
    @category = Category.new
    @all_sections = Section.where("active = 1 and section_order != 4 or (section_order = 4 and api_controller = 'DocumentController')").order(:section_order)
    @parent_categories = Category.where(:parent_category_id => nil).order(:section_order,:category_order,:name)    
  end

  # GET /categories/1/edit
  def edit
    @active_section = Section.find_by_short_name("Categorias")
    @category = Category.find(params[:id])   
    @all_sections = Section.where("active = 1 and section_order != 4 or (section_order = 4 and api_controller = 'DocumentController')").order(:section_order)
    @parent_categories = Category.where(:parent_category_id => nil).order(:section_order,:category_order,:name)
  end

  # POST /categories
  # POST /categories.xml
  def create       
    @category = Category.new(params[:category])
    @category.section_order = Section.find_by_id(@category.section_id).section_order

    respond_to do |format|
      if @category.save
        format.html { redirect_to(categories_url, :notice => 'Categoria fue creada exitosamente.') }        
      else
        format.html { render :action => "new" }
      end
    end
  end

  # PUT /categories/1
  # PUT /categories/1.xml
  def update   
    @category = Category.find(params[:id])

    respond_to do |format|
      if @category.update_attributes(params[:category])
        format.html { redirect_to(categories_url, :notice => 'Categoria fue actualizada exitosamente.') }        
      else
        format.html { render :action => "edit" }
      end
    end
  end

  # DELETE /categories/1
  # DELETE /categories/1.xml
  def destroy
    @category = Category.find(params[:id])
    @category.destroy

    respond_to do |format|
      format.html { redirect_to(categories_url) }
    end
  end

end
